// pages/loading/index.js
import { getLoadingAd } from '../../http/ad'
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseimgaeUrl: app.globalData.imageBaseUrl,
    staticUrl: app.globalData.staticUrl,
    lefttimer:3,
    lefttimerobj:'',
    url:'',
    loadingadurl :app.globalData.staticUrl + '/images/home.jpg?'+ new Date().getDay(),
    tab:'',
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var url = options.url;
    var tab =  options.tab;
    this.setData({url:url,tab:tab});
    
   /* var that = this;
    let {lefttimer,lefttimerobj} = this.data;
    lefttimerobj =   setInterval(function(){
        if(lefttimer>=0){
            that.setData({lefttimer:lefttimer--})
        }
        else{
            clearInterval(lefttimerobj);
            that.setData({lefttimerobj:''});
            that.hidehomepic();
        }
    },1000);
    this.setData({lefttimerobj:lefttimerobj});*/
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getLoadingAd();
    var that = this;
    let {lefttimer,lefttimerobj} = this.data;
    lefttimerobj =   setInterval(function(){
        if(lefttimer>=0){
            that.setData({lefttimer:lefttimer--})
        }
        else{
            clearInterval(lefttimerobj);
            that.setData({lefttimerobj:''});
            that.hidehomepic();
        }
    },1000);
    this.setData({lefttimerobj:lefttimerobj});
  },


   // 获取玩家信息
   async getLoadingAd() {
    
   
    const { data } = await getLoadingAd({})
    this.setData({
        lefttimer: data.lefttimer,
        loadingadurl :data.url
    })
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    let {lefttimer,lefttimerobj} = this.data;
 
    console.log('onHide',lefttimerobj);
    clearInterval(lefttimerobj);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    let {lefttimer,lefttimerobj} = this.data;
    console.log('onUnload',lefttimerobj);
    clearInterval(lefttimerobj);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  


  hidehomepic:function(){
    if(this.data.lefttimerobj){
       clearInterval(this.data.lefttimerobj);
       this.setData({lefttimerobj:'',lefttimer:6,});
    }

    let pages = getCurrentPages(); //当前页面栈
    if (pages.length >= 1) {
     //todo
     //if(pages[0].route != 'pages/loading/index'){
     wx.navigateBack({
       delta:-1
     })
   
     return;
    }
    var url = this.data.url;
    var tab = this.data.tab;
    if(!url){
      
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
    else{
      if(tab){
        wx.switchTab({
          url: url,
        })
      }
      else{
        wx.redirectTo({
       
          url: url,
        })
      }
    }
   },
})